package com.simple.database.controller;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.simple.database.pojo.Blog;
import com.simple.database.service.BlogService;
import com.simple.database.utils.PageUtil;
import com.simple.database.utils.RedisUtil;

/**
 * 查询所有博客内容123456
 */
@Controller
@RequestMapping("blog")
public class BlogController {

	@Autowired
	private BlogService blogService;
	
	private static final Logger LOGGER = LoggerFactory
			.getLogger(BlogController.class);
	
	@RequestMapping("insert")
	@ResponseBody
	public String insertData() throws Exception{
			Long startTime = System.currentTimeMillis();
			Set<String> keys = RedisUtil.keys("*");
			Iterator<String> iterator = keys.iterator();
			while(iterator.hasNext()){
				String next = iterator.next();
				String jsonValue = RedisUtil.get(next);
				Blog blog = JSONObject.parseObject(jsonValue, Blog.class);
				blog.setId(null);
				blog.setSid(2);
				int result = blogService.insert(blog);
				if(result>0){
					System.out.println("添加成功："+blog.getUrl());
				}else{
					System.out.println("添加失败："+blog.getUrl());
				}
			}
			Long endTime = System.currentTimeMillis();
			System.out.println("添加数据完毕，花费时间："+(endTime-startTime)/1000+"秒");
			return "添加数据完毕，花费时间："+(endTime-startTime)/1000+"秒";
	}
	
	/**
	 * 多条件组合查询博客
	 * @param blogParam
	 * @return
	 */
	@RequestMapping(value="query", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> queryAllBlogByParams(Blog blog,PageUtil pageUtil){
		LOGGER.info(">>>>>>>>> blogParam = "+blog+">>>>>>>> pageUtil = "+pageUtil);
		try {
			if(null==pageUtil.getPage()){
				pageUtil.setPage(1);
			}
			if(null==pageUtil.getRows()){
				pageUtil.setRows(10);
			}
			 Integer count = this.blogService.queryAllBlogCounts(blog);
			 LOGGER.info(">>>>>>>>> 共查询到："+count+"条博客记录");
			if(count>0){
				Map<String,Object> maps = this.blogService.queryAllBlog(blog,pageUtil);
				LOGGER.info(">>>>>>>>> 查询结果 = "+maps);
				return maps;
			}
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("多条件组合查询博客异常！");
		}
		return null;
	}
}
